<?php
/**
 *
 * @notes:
 * @author:     Jokenzhang
 * @time:       2020/12/23  9:59 下午
 * @version:    V20201223.1.1
 *
 */


namespace Modules\Article\Service;


use Houdunwang\Arr\Arr;

class TagService
{

    //服务调用
    public function make()
    {
        $this->slide();
        $this->category();
        $this->list();
    }

    //文章标签
    public function list()
    {
        \Blade::directive('list', function ($expression) {
            $expression = $expression ?: "[]";
            $list = <<<php
            <?php
                \$params = $expression;
                \$data = new \Modules\Article\Entities\Content();
                if(isset(\$params['istop'])){
                    \$data =\$data -> where('istop','=',1);
                }
                if(isset(\$params['ishot'])){
                \$data =\$data->orderBy('click','desc');
                }
                if(isset(\$params['limit'])){
                \$data = \$data -> limit(\$params['limit']);
                }
                if(isset(\$params['cid'])){
                \$data = \$data->whereIn('category_id',\$params['cid']);
                }
                foreach (\$data->get() as \$field):

                \$field['url'] = '/article/content/' .\$field['id']. '.html';

            ?>
php;

            return $list;
        });
        \Blade::directive('endList', function () {
            return "<?php endforeach; ?>";
        });
    }

    //栏目的标签
    public function category()
    {
        \Blade::directive('category', function ($expression) {
            $expression = $expression ?: "[]";
            $category = <<<php
            <?php
                \$params = $expression;
                \$data =  \Modules\Article\Entities\Category::query()->get()->toArray();
                \$data = (new Houdunwang\Arr\Arr())->channelList(\$data,0,"&nbsp;",'id');
                foreach(\$data as \$cate):
                \$cate['url'] = '/article/list/' .\$cate['id']. '.html';
            ?>
php;
            return $category;
        });
        \Blade::directive("endCategory", function () {
            return "<?php endforeach; ?>";
        });
    }


    //幻灯片标签
    public function slide()
    {
        //自定义标签方法 @slide
        \Blade::directive('slide', function ($expression) {
            $expression = $expression ?: '["width" => 100,"height" => 100]';

            $slide = <<<"php"

            <div class="swiper-container">
                <div class="swiper-wrapper">
                <?php
                        \$param = $expression;
                      \$data =  \Modules\Article\Entities\Slide::where('status',1)->get();
                      foreach(\$data as \$img):
                      echo  '<div class="swiper-slide"><a href="' .\$img['url'] . '">
                      <img src="' . \$img['img'] . '" alt=""></a> </div>';
                      endforeach;
                ?>
                        </div>

                        <div class="swiper-pagination"></div>
                        <div class="swiper-button-prev"></div>
                        <div class="swiper-button-next"></div>

                    </div>
                    <style>
                        .swiper-container {
                            width: 100%;
                            height: <?php echo \$param['height']; ?>;
                        }
                        .swiper-container img{
                            width:100%;
                            height:<?php echo \$param['height']; ?>;
                        }
                    </style>
                    <script>
                        var mySwiper = new Swiper('.swiper-container', {
                            loop: true, // 循环模式选项
                            autoplay:true,

                            // 如果需要分页器
                            pagination: {
                                el: '.swiper-pagination',
                            },

                            // 如果需要前进后退按钮
                            navigation: {
                                nextEl: '.swiper-button-next',
                                prevEl: '.swiper-button-prev',
                            },

                        })
                    </script>

php;
            return $slide;
        });
    }
}
